<template>
    <van-nav-bar title="购物车" left-text="返回" left-arrow @click-left="() => { $router.push('/') }" />
    <div style="height: 45px;"></div>
    <van-empty v-if="!cartData.length" image="https://fastly.jsdelivr.net/npm/@vant/assets/custom-empty-image.png"
        image-size="80" description="购物车空空" />
    <div v-for="item in cartData">
        <van-swipe-cell>
            <van-card :price="item.goods.goods_price" :title="item.goods.goods_name">
                <template #thumb>
                    <div style="display: flex;">
                        <van-checkbox v-model="item.checked"></van-checkbox>
                        <img :src="item.goods.goods_small_logo ? item.goods.goods_small_logo : 'https://fastly.jsdelivr.net/npm/@vant/assets/ipad.jpeg'"
                            alt="" style="width: 70px;height: 70px;">
                    </div>
                </template>
                <template #num>
                    <van-stepper v-model="item.number" theme="round" button-size="22" disable-input />
                </template>
            </van-card>
            <template #right>
                <van-button square text="删除" type="danger" class="delete-button" @click="del(item.id)"
                    style="height: 100%;" />
            </template>
        </van-swipe-cell>
        <van-submit-bar :price="allprice" button-text="提交订单" @submit="onSubmit">
            <van-checkbox v-model="checkedall">全选</van-checkbox>
        </van-submit-bar>
    </div>
</template>

<script setup>
import { useCounterStore } from '@/stores/piniaCart'
import { ref, computed } from 'vue'
// 在组件内部的任何地方均可以访问变量 `store` ✨
const store = useCounterStore()
let cartData = ref([])
const cart = () => {
    cartData.value = store.piniaCart
}
cart()
const del = (id) => {
    console.log('您点击了删除',id)
    store.piniaCart = store.piniaCart.filter(item => item.id != id)
    cart()
}
let checkedall = computed({
    get() {
        if (cartData.value.length == 0) {
            return false
        } else {
            return cartData.value.every(item => item.checked)
        }
    },
    set(val) {
        cartData.value.forEach(item => item.checked = val)
    }
})
let allprice = computed(() => {
    let allprice = ref(0)
    let cartOk = store.piniaCart.filter(item => item.checked)
    cartOk.forEach(item => {
        allprice.value = (item.goods.goods_price * 10 / 10) * item.goods.goods_price
    })
    allprice.value * 100
})
</script>

<style lang="scss" scoped></style>